package com.itboye.jinpuyiyuan.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.pay.PayActivity;
import com.itboye.jinpuyiyuan.adapter.LaborDetailAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.LaborInfoBean;
import com.itboye.jinpuyiyuan.bean.OrderDetailBean;
import com.itboye.jinpuyiyuan.bean.RecipeOrderBean;
import com.itboye.jinpuyiyuan.bean.RecipeRecordInfo;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class PrescriptionRecordInfo extends BaseActivity implements Observer {

    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;


    private LinearLayout inhosLl,//住院
            laborLl, //化验
            mediLl; //处方

    private TextView med_Time;
    private TextView med_name,med_yb,med_sj,med_amount;
    private TextView fee,paySum,prepaySum;

    private TextView med_fb,med_mc,med_lsh,med_dj,med_type,med_zje,med_yibao;
    private TextView toPayTv;

    private BranchAddressPresenter presenter;
    private String uid;
    private String hosp_sequence;
    private String order_stutas,payType,date;
    private LaborInfoBean bean;
    private List<LaborInfoBean.Check.Detail> list;

//intent.putExtra("hosp_sequence",list.get(position).getHosp_sequnce());
//                    intent.putExtra("order_stutas",list.get(position).getOrder_status());
//                    intent.putExtra("payType",list.get(position).getPay_channel_id());
//                    intent.putExtra("date",list.get(position).getReceipt_date());
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_hospitaldetail);
        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("处方记录详情");

        list=new ArrayList<>();

        hosp_sequence=getIntent().getStringExtra("hosp_sequence");
        order_stutas=getIntent().getStringExtra("order_stutas");
        payType=getIntent().getStringExtra("payType");
        date=getIntent().getStringExtra("date");

        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);

        mediLl.setVisibility(View.VISIBLE);
        inhosLl.setVisibility(View.GONE);
        laborLl.setVisibility(View.GONE);


        showProgressDialog("数据加载中...",false);
        presenter.recipeRecordInfo(uid,hosp_sequence);

    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity!=null){
            try{
                closeProgressDialog();
            }catch (Exception e){

            }
            if (entity.getEventType() == BranchAddressPresenter.By_prescriptionDetail_success){ //订单详情
//                orderNumTv,//订单编号
//                        orderTimeTv,  //订单时间
//                        orderDetailName, //订单详情
//                        payManeyTv,//支付金额
//                        payTypeTv;//支付类型
                RecipeRecordInfo info=  (RecipeRecordInfo) entity.getData();
//                private TextView med_Time;
//                private TextView med_name,med_yb,med_sj,med_amount;
//                private TextView fee,payByMe,prepaySum;
//
//                private TextView med_fb,med_mc,med_lsh,med_dj,med_type,med_zje,med_yibao;
//                private TextView toPayTv;
                med_Time.setText(date);
                med_name.setText(info.getUser_name());
                med_yb.setText(info.getMedical_insurannce_type());
                med_sj.setText(info.getReceipt_id());
                med_amount.setText(Float.parseFloat(info.getPay_total_fee())/100+"");
                fee.setText(Float.parseFloat(info.getPay_behoove_fee())/100+"");
                paySum.setText(Float.parseFloat(info.getPay_actual_fee())/100+"");
                prepaySum.setText(Float.parseFloat(info.getPay_mi_fee())/100+"");
                med_fb.setText(info.getPay_detail_list().get(0).getDetail_type());
                med_mc.setText(info.getPay_detail_list().get(0).getDetail_name());
                med_lsh.setText(info.getPay_detail_list().get(0).getDetail_id());
                med_dj.setText(Float.parseFloat(info.getPay_detail_list().get(0).getDetail_price())/100+"");

                med_type.setText(info.getPay_detail_list().get(0).getDetail_spec());
                med_zje.setText(Float.parseFloat(info.getPay_detail_list().get(0).getDetail_amout())/100+"");
                med_yibao.setText(Float.parseFloat(info.getPay_detail_list().get(0).getDetail_mi())/100+"");

                //收据号
//                orderNumTv.setText(data.getReceipt_id());
//                orderTimeTv.setText(date);
//
//                orderDetailName.setText("");
                if (order_stutas.equals("1")){//已付款
                    toPayTv.setVisibility(View.GONE);
                }else {//未付款
                    toPayTv.setVisibility(View.VISIBLE);
                }
//                DecimalFormat df = new DecimalFormat("#0.00");
//                payManeyTv.setText(df.format(Double.parseDouble(data.getPay_behoove_fee())/100));
//                if (payType.trim().equals("")){
//                    toPay.setVisibility(View.VISIBLE);
//                    payTypeLl.setVisibility(View.GONE);
//                }else {
//                    toPay.setVisibility(View.GONE);
//                    payTypeLl.setVisibility(View.VISIBLE);
//                    if (payType.trim().equals("1")){//微信
//                        payTypeTv.setText("微信支付");
//                    }else if (payType.trim().equals("2")){//支付宝
//                        payTypeTv.setText("支付宝支付");
//                    }else if (payType.trim().equals("6")){
//                        payTypeTv.setText("医院窗口支付");
//                    }
//                }

            }else if (entity.getEventType()== BranchAddressPresenter.By_prescriptionDetail_fail){

                MAlert.alert(entity.getMsg());
            }else if (entity.getEventType()== BranchAddressPresenter.By_Hospitalization_prescriptionOrder_success){
                toPayTv.setEnabled(true);
                RecipeOrderBean bean= (RecipeOrderBean) entity.getData();
                Intent intent = new Intent(this, PayActivity.class);
                intent.putExtra("order_id",bean.getOrder_code());
                intent.putExtra("remark",bean.getNote());
                startActivity(intent);
            }else if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_prescriptionOrder_fail){
                toPayTv.setEnabled(true);
                MAlert.alert(entity.getMsg());
            }
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
            case R.id.toPayTv:
                toPayTv.setEnabled(false);
                presenter.recipeOrder(uid,hosp_sequence);
                break;
        }
    }

}
